﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TangentAndDOA
{
    public partial class Form1 : Form
    {
        Round round = new Round();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String[] s1 = O.Text.Split(';');
            round.Ox = double.Parse(s1[0]);
            round.Oy = double.Parse(s1[1]);
            round.R = double.Parse(r.Text);
            round.X = double.Parse(x.Text);
            //根据圆的方程公式（x-Ox)²+（y-Oy）²=R²
            //推出 y²-2*Oy*y+x²-2*Ox*x+Ox²+Oy²-R²=0 ==>除变量y除外其余都是已知 根据求根公式求出y值
            double a = 1;
            double b = 2 * round.Oy;
            double c = Math.Pow(round.X, 2.0) - 2 * round.Ox * round.X + Math.Pow(round.Ox, 2.0) + Math.Pow(round.Oy, 2.0) - Math.Pow(round.R, 2.0);
            double y1 = (-b + Math.Sqrt(Math.Pow(b, 2.0) - 4 * a * c)) / 2 / a;
            double y2 = (-b - Math.Sqrt(Math.Pow(b, 2.0) - 4 * a * c)) / 2 / a;
            point1.Text = round.X.ToString() + ';' + y1.ToString();
            point2.Text = round.X.ToString() + ';' + y2.ToString();
            //圆心与遇上一点的直线的斜率k=tan值
            double k1 = (y1 - round.Oy) / (round.X - round.Ox);
            double k2 = (y2 - round.Oy) / (round.X - round.Ox);
            //切线的斜率为-1/k
            double k3 = -1 / k1;
            double k4 = -1 / k2;
            double angle1 = Math.Atan(k3) * 180 / Math.PI;
            double angle2 = Math.Atan(k4) * 180 / Math.PI;
            if (round.X>round.Ox+round.R || round.X < round.Ox - round.R)
            {
                tips.Text = "X轴取值范围为：" + (round.Ox + round.R).ToString() + '>' +'X' + '>' + (round.Ox - round.R).ToString()+",请重新输入";
            }
            else if(k1==0 ||k2==0)
            {
                doa1.Text = "90°";
                doa2.Text = "270°";

            }
            else
            {
                if (angle1 < 0)
                {
                    angle1 = angle1 + 360;
                }
                if (angle2<0)
                {
                    angle2 = angle2 + 360;
                }
                doa1.Text = angle1.ToString() + "°";
                doa2.Text = angle2.ToString() + "°";
            }
           
            
               

                    
                

            

        }

        private void label3_Click(object sender, EventArgs e)
        {
          
        }

        private void O_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号和分号  
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-') && e.KeyChar != (char)(';'))
            {
             
                e.Handled = true;
            }
            if (e.KeyChar == (char)('-'))
            {
                if (O.Text != "")
                {
                    
                    e.Handled = true;
                }
            }

            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {
               
                
                e.Handled = true;
            }
            /*第一位不能为分号*/
            if (e.KeyChar == (char)(';') && ((TextBox)sender).Text == "")
            {
                
                
                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text.IndexOf(';') != 1)
            {
                e.Handled = true;
            }
            






        }

        private void r_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.'))
            {

                e.Handled = true;
            }
            
            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
           
            //第1位是0，第2位必须是小数点
            if (e.KeyChar != (char)('.') && e.KeyChar != 8 && ((TextBox)sender).Text == "0")
            {
                e.Handled = true;
            }
        }

        private void x_KeyPress(object sender, KeyPressEventArgs e)
        {
            //允许输入数字、小数点、删除键、负号
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != (char)('.') && e.KeyChar != (char)('-'))
            {

                e.Handled = true;
            }
            if (e.KeyChar == (char)('-'))
            {
                if (x.Text != "")
                {

                    e.Handled = true;
                }
            }

            /*第一位不能为小数点*/
            if (e.KeyChar == (char)('.') && ((TextBox)sender).Text == "")
            {


                e.Handled = true;
            }
            //第1位是负号时候、第2位小数点不可
            if (((TextBox)sender).Text == "-" && e.KeyChar == (char)('.'))
            {
                e.Handled = true;
            }
            
            
            //第1位是0，第2位必须是小数点
            if (e.KeyChar != (char)('.') && e.KeyChar != 8 && ((TextBox)sender).Text == "0")
            {
                e.Handled = true;
            }
        }

        private void point1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (true)
            {
                e.Handled = true;
            }
        }

        private void point2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (true)
            {
                e.Handled = true;
            }
        }
    }
}

